<?php
class popuploginplugin extends WikiPlugin
{

	function popuploginplugin()
	{
		global $session;

		if(!$session->is_auth()) {
			$this->accumulate('header', 'popuplogin', 'popuplogin');
			$this->accumulate('header', 'jquery', 'jquery');
		}
		$this->add_ajax_handler('popuplogin', 'ajax');
	}

	function popuplogin()
	{
		global $session, $rewrite;

		return '
			<script type="text/javascript">
				var popwindow = false;
				var ajaxurl="' . $rewrite->get_link(array('special'=>'ajax')) . '";
				function popupLogin(e) {
					e.preventDefault( );

					if(!popwindow) {
						$("body").append(\'<div id="popuplogin" style="display:none">Loading...</div>\');
					}
					$.post(
						ajaxurl,
						{
							handler: "popuplogin",
							action: "showform",
							forward: $(e.target).attr("href")
						},
						function(r){
							$("#popuplogin").html(r);
						}
					);
					popwindow = true;
					$("#popuplogin").show();
				}
				function doLogin(e) {
					e.preventDefault( );

					$.post(
						ajaxurl,
						{
							handler: "popuplogin",
							action: "login",
							microsession_user: $("#microsession_user").val(),
							microsession_pass: $("#microsession_pass").val(),
							forward: $("#popuploginforward").val()
						},
						function(r){
							$("#popuplogin").html(r);
						}
					);
				}
				$(document).ready(function(){
					$("a").each(function(){
						if($(this).attr("href").match(/\/edit$/)) {
							$(this).click(popupLogin);
						}
					});
				});
			</script>
		';
	}

	function jquery()
	{
		global $rewrite;

		return '<script type="text/javascript" src="' . $rewrite->get_link(array('page'=>'js/jquery-1.2.2.min.js')) . '"></script>';
	}

	function ajax()
	{
		global $wiki, $mwconfig, $session, $events;

		switch($_POST['action']) {
		case 'showform':
			$output = $wiki->getpage("pagename={$mwconfig->loginpage}&useformat=", $pagerow);
			$output .= '
			<script type="text/javascript">
			$("#microauth").append("<input type=\'hidden\' id=\'popuploginforward\' name=\'forward\' value=\'' . $_POST['forward'] . '\'>");
			$("#microauth").submit(doLogin);
			$("#microsession_user").focus();
			</script>
			';
			break;
		case 'login':
			if($session->is_auth()) {
				$output = '<p>Login successful...</p>
				<script type="text/javascript">
				var die = false;
				$("a").each(function(){
					$(this).unbind("click");
				});
				';

				foreach((array)$events['after_popup_login'] as $fn) {
					$output .= call_user_func($fn);
				}

				$output .='
				if(!die) location.href="' . $_POST['forward'] . '";
				</script>
				';
			}
			else {
				$output = '<p>That login was incorrect.  Please try again.</p>';
				$output .= $wiki->getpage("pagename={$mwconfig->loginpage}&useformat=", $pagerow);
				$output .= '
				<script type="text/javascript">
				$("#microauth").append("<input type=\'hidden\' id=\'popuploginforward\' name=\'forward\' value=\'' . $_POST['forward'] . '\'>");
				$("#microauth").submit(doLogin);
				$("#microsession_user").focus();
				</script>
				';
				break;
			}
			break;
		}

		echo $output;
	}
}
?>
